﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using CharacterTrailClient.Message;
using CharacterTrailClient.Query.Item;
using CharacterTrailClient.UiControl;
using CharacterTrailClient.Windows.Exceptions;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Impl;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.Utils;
using Telegnosis.Framework.Constants;
using Telegnosis.Framework.UI.Util;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;
using System.IO;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraReports.UserDesigner;

namespace CharacterTrailClient.Windows
{
    public partial class FormBom : FormBase
    {
        private readonly IExcelImportor _excelImportor = null;
        private readonly IBomHeaderPersist _bomHeaderPersist = null;
        private readonly IBomTreeControl _bomTreeControl = null;
        private DataTable _dataTable = null;
        public FormBom()
        {
            InitializeComponent();
            _excelImportor = InstanceFactory.GetExcelImportor();
            _bomHeaderPersist = InstanceFactory.GetBomHeaderPersis();
            _bomTreeControl = new BomTreeControl();
        }

        public override string HeaderText
        {
            get
            {
                return "      Bom     ";
            }
        }

        private void CmdOpenFile_Click(object sender, EventArgs e)
        {
            try
            {
                var openFile = new OpenFileDialog();
                openFile.Filter = @"*.xls|*.xls";
                if (DialogResult.OK == openFile.ShowDialog())
                {
                    _dataTable =  _excelImportor.GetDataTable(openFile.FileName);
                    gridControlBomImport.DataSource = _dataTable;
                    gridControlBomImport.RefreshDataSource();
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdImport_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridControlBomImport.DataSource != null
                    && gridControlBomImport.DataSource is DataTable)
                {
                    _excelImportor.ImportToDb(_dataTable);
                    MsgBox.ShowInfomation("导入成功！");
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdClear_Click(object sender, EventArgs e)
        {
            if (_dataTable != null)
            {
                gridControlBomImport.DataSource = null;
                gridControlBomImport.RefreshDataSource();
            }
        }
    
        private void CmdQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string conditon = "  2 =2 ";
                foreach (Control control in this.splitContainerControl1.Panel1.Controls)
                {
                    if(control is IQueryItem)
                    {
                        var queryItem = (IQueryItem) control;
                        conditon += queryItem.GetCondition();
                    }
                }

                var list = _bomTreeControl.GetTBomHeaderModelsByCondition(conditon);
                this.gridControlBomEdit.DataSource = list;
            }
            catch (Exception exception)
            {
              ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdDeleteSelect_Click(object sender, EventArgs e)
        {
            try
            {
                var rows = this.gridViewBomImport.GetSelectedRows();
                var dataRows = new List<DataRow>();
                foreach (int row in rows)
                {
                    DataRow dataRow = this.gridViewBomImport.GetDataRow(row);
                   dataRows.Add(dataRow);
                }
                foreach (var dataRow in dataRows)
                {
                     _dataTable.Rows.Remove(dataRow);
                }
               
                _dataTable.AcceptChanges();
                this.gridControlBomImport.RefreshDataSource();
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdNew_Click(object sender, EventArgs e)
        {
            var form = new FormBomEdit();
            form.StartPosition = FormStartPosition.CenterParent;
            if(System.Windows.Forms.DialogResult.OK == form.ShowDialog(this) )
            {
                var list = _bomTreeControl.GetTBomHeaderModels();
                this.gridControlBomEdit.DataSource = list;
            }
        }

        private void CmdDelete_Click(object sender, EventArgs e)
        {
            try
            {
                var selectedRows = this.gridViewBomEdit.GetSelectedRows();
                var list = new List<TBomHeaderModel>();
                foreach (var selectedRow in selectedRows)
                {
                    var model = (TBomHeaderModel)this.gridViewBomEdit.GetRow(selectedRow);
                    model.EditFlag = Enums.EditFlag.Delete;
                    list.Add(model);
                }
                IBomHeaderPersist bomHeaderPersist = InstanceFactory.GetBomHeaderPersis();
                if (Message.MsgBox.ShowYesOrNo(String.Format("确实要删除选中的{0}条记录吗？", list.Count)))
                {
                    bomHeaderPersist.DeleteBomHeaders(list);
                    this.gridControlBomEdit.DataSource = _bomTreeControl.GetTBomHeaderModels();
                }
            }
            catch (Exception exception)
            {
                Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        private void CheckEditMergeCheckedChanged(object sender, EventArgs e)
        {
            this.gridViewBomEdit.OptionsView.AllowCellMerge = this.checkEditMerge.Checked;
            //var gridColumn = this.gridViewBomEdit.Columns.ColumnByFieldName("ParentItemNumber");
            //gridColumn.OptionsColumn.AllowMerge = this.checkEditMerge.Checked ? DefaultBoolean.True : DefaultBoolean.False;
            //gridColumn = this.gridViewBomEdit.Columns.ColumnByFieldName("ParentItemName");
            //gridColumn.OptionsColumn.AllowMerge = this.checkEditMerge.Checked ? DefaultBoolean.True : DefaultBoolean.False;
        }
    }
}